package com.rural.bus;

import org.json.JSONException;
import org.json.JSONObject;

import com.rural.bus.model.Order;
import com.rural.bus.net.MyHttpUtil;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.net.Url;
import com.rural.bus.utils.ProgressUtil;
import com.rural.bus.view.InputDialog;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/***
 * 支付页
 * @author majie
 *
 */

public class PayActivity extends BaseActivity {
	
	private TextView tvTotalPrice;
	
	private Button mBtPay,mBtCashPay;
	
	private Order order;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay);
		
		initView();
		setListeners();
		initData();
	}

	@Override
	protected void initView() {
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setText("确认支付");
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
		
		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
		
		tvTotalPrice=(TextView) findViewById(R.id.tv_total_price);
//		tvDistance=(TextView) findViewById(R.id.tv_distance);
//		tvDuration=(TextView) findViewById(R.id.tv_duration);
		
		mBtPay=(Button) findViewById(R.id.btn_pay);
		mBtCashPay=(Button) findViewById(R.id.btn_cash_pay);

	}

	@Override
	protected void initData() {

		order=(Order) getIntent().getSerializableExtra("order");
		if(order!=null){
		    tvTotalPrice.setText("费用总计：   "+order.estimatePrice+" 元");
		}
	}

	@Override
	protected void setListeners() {
		
		mBtPay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				InputDialog dialog=new InputDialog(PayActivity.this, "是", "否", "", new InputDialog.OnInputDialogDismissListener() {
					
					@Override
					public void onConfirmClicked(String msg) {
						pay(msg);
					}

					@Override
					public void onCancelClicked() {
						
					}
				});
				dialog.show();
			}
		});
		
        mBtCashPay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				pay(null);
			}
		});
        
        mImgLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				PayActivity.this.finish();
			}
		});
		
	}
	
	/***
	 * 支付
	 * @param passwd 密码为空，为在线支付
	 */
	private void pay(String passwd){
		ProgressUtil.showProgress(this, "");
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("orderId", ""+order.orderId);
		http.addBodyParm("uid", ""+mCurrentUser.userId);
		if(passwd!=null){
			http.addBodyParm("payMode", "1");
			http.addBodyParm("password", passwd);
		}else{
			http.addBodyParm("payMode", "0");
		}

		http.send(Url.finish_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				Log.i("pay", object.toString());
				try {
					if(object.getInt("callStatus")==-1004){
						toast("余额不足");
					}else if(object.getInt("callStatus")==-1003){
						toast("密码不对");
					}else{
						toast("支付成功");
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				toast(error);
			}
		});
	}
	
	@Override
	public void finish() {
		super.finish();
		setResult(0x01);
	}

}
